home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / VideoToolbox 94.11.17 / Demos / Sandstorm.c < prev    next >
Encoding:
Text File  |  1994-11-15  |  10.4 KB  |  299 lines  |  [TEXT/KAHL]

  1.  random
  2. offset within that noise.
  3.  
  4. Note: the amount of data that has to be transfered by CopyBitsQuickly() in each
  5. frame is proportional to window area and the bits/pixel of the display, as set by
  6. Control Panel Monitors. At 8 bits/pixel the maximum window that can be updated
  7. at 66 Hz is about 192x192. At 1 bit/pixel we can do at least 480x640.
  8.  
  9. Sandstorm--not CopyBitsQuickly--calls WaitNextEvent() before each
  10. frame, in order to be responsive to the mouse, and this results in occasional
  11. pauses as the Mac takes time out to do various chores.
  12.  
  13.  
  14. HISTORY:
  15. 11/23/88 dgp derived from truenoiseDemo2.c
  16. 4/7/89 dgp     I removed nearly all the hardware dependencies. The only remaining one
  17.             is the call to NewFieldTFB(). I don't know how to get rid of that, short
  18.             of writing a VBL task.
  19. 9/29/89 dgp I've nearly updated this to THINK C 4.0, but the call to TickCount, 
  20.             refers to the old profiler, and I don't have time to figure this out now.
  21. 10/28/89 dgp I replaced the call to NewFieldTFB() by a call to GDSetEntries() which
  22.             is a device-independent way to wait for the end of frame.
  23. 7/19/90 dgp Unfortunately the built-in video on the Mac IIci is buggy and doesn't
  24.             support the setEntries call.
  25. 10/17/90 dgp Yay. Finally removed last bugs! Replaced SlotToScreenDevice() by
  26.             AddressToScreenDevice() in order to make it compatible with built-in video
  27.             on Mac IIci, IIsi, and LC. Removed the setEntries call for compatibility
  28.             with Mac IIci built-in video.(Apple has acknowledged bug, but bug is
  29.             still present in System 6.07.) Fixed bug whereby resizing window on other
  30.             than the main screen left window blank until it was dragged again. 
  31.             Removed unused variables. Added zoom box.
  32. 10/18/90 dgp Tidied up. Now zooms onto screen with largest intersection, and zooms back
  33.             to original size. Uses as much memory as user allocates to program. 
  34.             Window now always confines itself to a single screen, but can be dragged
  35.             anywhere.
  36. 10/20/90 dgp Copied code from VideoTest to close window and quit on Command-W or
  37.             Command-. Prevented window title bar from being hidden behind the menu
  38.             bar.
  39. 7/22/91    dgp    Made compatible with MPW C. This required changing INT_MAX to SHRT_MAX.
  40.             However, performance is slow, since CopyBitsQuickly.c doesn't generate
  41.             hand-tuned assembly code when using MPW C.
  42. 8/6/91    dgp    Replaced randU() by RandFill(), which runs twice as fast.
  43. 8/24/91    dgp    Made compatible with THINK C 5.0.
  44. 7/20/92    dgp    tidied up the comments slightly. Confirmed compatibility with 32-bit
  45.             addressing.
  46. 8/27/92    dgp    replace SysEnvirons() by Gestalt()
  47. 1/13/93 dgp    changed defaults to maximize speed.
  48. 9/3/93    dgp    added call to ShieldCursor() for compatibility with nonstandard
  49.             video devices, e.g. Radius PowerView.
  50. 6/3/94    dgp    replaced low-memory global MBarHeight by GetMBarHeight().
  51. 9/5/94 dgp removed assumption in printf's that int==short.
  52. */
  53. #include "VideoToolbox.h"
  54. #if UNIVERSAL_HEADERS
  55.     #include <LowMem.h>
  56. #else
  57.     #define LMGetMBarHeight() (* (short *) 0x0BAA)
  58.     #define LMSetMBarHeight(MBarHeightValue) ((* (short *) 0x0BAA) = (MBarHeightValue))
  59. #endif
  60. void main(void);
  61. Boolean ZoomedOut(WindowPtr w);
  62. #define RAISE_PRIORITY    0    /* Optional. Contrary to Apple's rules,    */
  63.                             /* but speeds up the display. */
  64. #define SCROLL_BAR 15        /* Standard width of scroll bar */
  65. #define WAIT_NEXT_EVENT 0
  66.  
  67. void Sandstorm(void);
  68.  
  69. void main(void)
  70. {
  71.     Require(gestalt8BitQD);
  72.     Sandstorm();
  73. }
  74. void Sandstorm()
  75. {
  76.     register short *bufferPtr;
  77.     int i;
  78.     static char string[40];
  79.     static PixMap sand;
  80.     Rect r,unzoomedRect,gdRect;
  81.     GDHandle device;
  82.     WindowPtr window,aWindow,oldPort;
  83.     PixMap **myPixMapHandle;
  84.     static BitMap buffer;
  85.     long bufferBytes;
  86.     size_t sandBytes;
  87.     Boolean done=0,update;
  88.     long reSize;
  89.     int windowEvent=inDesk;
  90.     Boolean zoomedOut;
  91.     WStateData *wStateData;
  92.     RgnHandle ignoreRgn;
  93.     int pixelSize;
  94.     Point pt;
  95.     EventRecord event;
  96.     
  97.     /* INITIALIZE QuickDraw */
  98.     MaxApplZone();                        /* Expand heap to the limit. */
  99.     InitGraf(&qd.thePort);
  100.     InitFonts();
  101.     InitWindows();
  102.     InitCursor();
  103.     GetPort(&oldPort);
  104.     device=GetMainDevice();
  105.  
  106.     /* open a window on the device */
  107.     ignoreRgn=NewRgn();
  108.     SetRect(&r,0,0,192+SCROLL_BAR,192+SCROLL_BAR);
  109.     CenterRectInRect(&r,&(*device)->gdRect);
  110.     window=NewCWindow(NULL
  111.         ,&r,"\pNow computing noise ...",TRUE,zoomDocProc,(WindowPtr) -1L,TRUE,0L);
  112.     myPixMapHandle = ((CGrafPtr)window)->portPixMap;
  113.     MoveHHi((Handle) myPixMapHandle);
  114.     HLock((Handle) myPixMapHandle);
  115.  
  116.     /* Compute noise */
  117.     /* allocate buffer BitMap */
  118.     buffer.rowBytes=1024;
  119.     MaxApplZone();
  120.     bufferBytes = FreeMem();
  121.     bufferBytes-=100000L;    /* Grab most of available space. */
  122.     bufferBytes -= bufferBytes%buffer.rowBytes;
  123.     buffer.baseAddr=NULL;
  124.     for(;bufferBytes>0;bufferBytes -= buffer.rowBytes){
  125.         buffer.baseAddr = NewPtr(bufferBytes);
  126.         if (buffer.baseAddr!=NULL)break;
  127.     }
  128.     if(buffer.baseAddr==NULL)PrintfExit("\007Sorry, not enough memory for buffers.\n");
  129.     SetRect(&buffer.bounds,0,0,buffer.rowBytes*8L,bufferBytes/buffer.rowBytes);
  130.  
  131.     /* Fill buffer with noise. */
  132.     bufferPtr = (short *) buffer.baseAddr;
  133.     RandFill(buffer.baseAddr,bufferBytes);
  134.  
  135.     /* Show noise */
  136.     SetPort(window);
  137.     reSize=0;
  138.     while (1) {
  139.         wStateData=(*((WStateData **)((WindowPeek)window)->dataHandle));
  140.         unzoomedRect=wStateData->userState;
  141.         zoomedOut=ZoomedOut(window);
  142.         if(reSize){
  143.             SizeWindow(window,LoWord(reSize),HiWord(reSize),TRUE);
  144.             reSize=0L;
  145.         }
  146.         device=GetWindowDevice(window);
  147.         if(device==NULL){
  148.             done=1;
  149.             break;
  150.         }
  151.         r=window->portRect;
  152.         LocalToGlobalRect(&r);
  153.         gdRect=(*device)->gdRect;
  154.         /* leave room for window's title bar */
  155.         gdRect.top+=r.top-1-(*((WindowPeek)window)->strucRgn)->rgnBBox.top;
  156.         /* leave room for menu bar */
  157.         if(device==GetMainDevice())gdRect.top+=LMGetMBarHeight();
  158.         SectRect(&gdRect,&r,&r);
  159.         pixelSize=(**(**device).gdPMap).pixelSize;
  160.         i=r.left-(*device)->gdRect.left;
  161.         i=(i*pixelSize+16 & ~31)/pixelSize-i;    /* move to nearest 32-bit boundary */
  162.         OffsetRect(&r,i,0);                        /* Assumes window is >16 bits wide */
  163.         SectRect(&gdRect,&r,&r);
  164.         MoveWindow(window,r.left,r.top,TRUE);
  165.         OffsetRect(&r,-r.left,-r.top);
  166.         sand = **(*device)->gdPMap;    /* Initialize PixMap fields from screen */
  167.         sand.bounds=r;
  168.         sand.bounds.right  -=SCROLL_BAR;    /* Leave room for the scroll bars */
  169.         sand.bounds.bottom -=SCROLL_BAR;
  170.         sand.bounds.right=(sand.bounds.right*sand.pixelSize+14 & ~31)/sand.pixelSize;
  171.             /* round to multiple of 32 bits */
  172.         if(sand.bounds.right<32)sand.bounds.right=32;
  173.         sand.rowBytes = (sand.bounds.right*sand.pixelSize + 31 & ~31)/8;
  174.             /* round up to multiple of 32 bits (redundant) */
  175.         sandBytes = (size_t) sand.rowBytes * (size_t) sand.bounds.bottom;
  176.         if(bufferBytes/2 < sandBytes)
  177.         {
  178.             /* restrict height by amount of noise available */
  179.             sand.bounds.bottom=bufferBytes/sand.rowBytes/2;
  180.             sandBytes = (size_t) sand.rowBytes * (size_t) sand.bounds.bottom;
  181.         }    
  182.         sand.rowBytes |= 0x8000;    /* Mark it as a PixMap */
  183.         sand.pmVersion=0;
  184.         sand.packType=0;
  185.         sand.packSize=0;
  186.         sand.planeBytes=0;
  187.         sand.pmReserved=0;
  188.         SizeWindow(window
  189.             ,sand.bounds.right+SCROLL_BAR,sand.bounds.bottom+SCROLL_BAR,TRUE);
  190.         EraseRect(&window->portRect);
  191.         DrawGrowIcon(window);
  192.         if(zoomedOut && windowEvent!=inGrow && windowEvent!=inDrag){
  193.             wStateData=(*((WStateData **)((WindowPeek)window)->dataHandle));
  194.             r=window->portRect;
  195.             LocalToGlobalRect(&r);
  196.             r.left-=1;
  197.             r.right-=2;
  198.             wStateData->stdState=r;        
  199.             wStateData->userState=unzoomedRect;
  200.         }
  201.         sprintf(string,"%dx%dx%d bits"
  202.             ,(int)sand.bounds.right,(int)sand.bounds.bottom,(int)sand.pixelSize);
  203.         SetWTitle(window,c2pstr(string));
  204.         FlushEvents(everyEvent,0);
  205.         DiffRgn(GetGrayRgn(),((WindowPeek)window)->strucRgn,ignoreRgn);
  206.         while (1) {
  207.             update=0;            /* don't update window frame unless necessary */
  208.             #if RAISE_PRIORITY
  209.                 SetPriority(2);    /* Get more speed by suppressing keyboard, mouse, and ticks. */
  210.             #endif
  211.             sand.baseAddr = buffer.baseAddr;
  212.             /* choose grain that won't overflow short argument to nrand() */
  213.             i=(bufferBytes/SHRT_MAX+3)&~3;    /* round up to multiple of 4 */
  214.             sand.baseAddr += nrand((bufferBytes-sandBytes)/i)*(long)i;
  215.             if(((WindowPeek)window)->hilited){
  216.                 CopyBitsQuickly((BitMap *)&sand,(BitMap *)*myPixMapHandle
  217.                     ,&sand.bounds,&sand.bounds,srcCopy,NULL);
  218.                 // Notify nonstandard video devices that pixels have changed.
  219.                 pt.h=-(*myPixMapHandle)->bounds.left;
  220.                 pt.v=-(*myPixMapHandle)->bounds.top;
  221.                 ShieldCursor(&sand.bounds,pt);
  222.                 ShowCursor();
  223.             }
  224.             #if RAISE_PRIORITY
  225.                 SetPriority(0);    /* Revive keyboard and mouse. */
  226.             #endif
  227.             #if WAIT_NEXT_EVENT
  228.                if(WaitNextEvent(mDownMask+keyDownMask,&event,0,ignoreRgn)){
  229.                #else
  230.                if(GetNextEvent(mDownMask+keyDownMask,&event)){
  231.                #endif
  232.                 switch(event.what){
  233.                 case keyDown:
  234.                     if(event.modifiers & cmdKey) switch(event.message & charCodeMask) {
  235.                         case '.':
  236.                         case 'w':
  237.                         done=1;
  238.                     }
  239.                     break;
  240.                 case mouseDown:
  241.                     update=1;
  242.                     windowEvent=FindWindow(event.where,&aWindow);
  243.                     if(aWindow==window){
  244.                         switch(windowEvent){
  245.                         case inDrag:
  246.                             DragWindow(window,event.where,&qd.screenBits.bounds);
  247.                             break;
  248.                         case inGoAway:
  249.                             done=TrackGoAway(window,event.where);
  250.                             break;
  251.                         case inGrow:
  252.                             r.top=r.left=32+SCROLL_BAR;    /* minimum size */
  253.                             r.bottom=r.right=1024+SCROLL_BAR;    /* max size */
  254.                             reSize = GrowWindow(window,event.where,&r);
  255.                             break;
  256.                         case inZoomIn:
  257.                         case inZoomOut:
  258.                             Zoom(window,windowEvent,&event);
  259.                             break;
  260.                         case inContent:
  261.                             break;
  262.                         default:
  263.                             update=0;
  264.                             break;
  265.                         }
  266.                         break;
  267.                     }
  268.                     break;
  269.                 }
  270.             }            
  271.             if(update || done)break;
  272.         }
  273.         if(done)break;
  274.     }
  275.     SetPort(oldPort);
  276.     DisposeRgn(ignoreRgn);
  277.     DisposPtr(buffer.baseAddr);
  278.     DisposeWindow(window);
  279. }
  280.  
  281. Boolean ZoomedOut(WindowPtr w)
  282. /* Determine whether window is "standard" size, i.e. zoomed out. */
  283. {
  284.     Point pt;
  285.     Rect r,myRect;
  286.     Boolean zoomedOut;
  287.     
  288.     myRect=(*((WStateData **)((WindowPeek)w)->dataHandle))->stdState;
  289.     pt.h=0;
  290.     pt.v=0;
  291.     LocalToGlobal(&pt);
  292.     r = w->portRect;
  293.     OffsetRect(&r,pt.h,pt.v);
  294.     zoomedOut=r.top==myRect.top
  295.         && r.left==myRect.left
  296.         && r.bottom==myRect.bottom
  297.         && r.right==myRect.right;
  298.     return zoomedOut;
  299. }